package com.jstyle.jclifexd.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jstyle.blesdk.Util.ResolveUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.utils.ScreenUtils;

import java.text.NumberFormat;

public class TemperatureView extends View {
    private Bitmap bitmap;
    Paint paintBg;
    Paint paintProgress;
    Paint paintText;
    int bitmapWidth;
    int bitmapHeight;
    int rectFWidth;
    int paddingBottom;
    int paddingTop;
    private int defaultHeight;
    private Bitmap bitmapTempC;
    private Bitmap bitmapTempF;
    private int offset;
    int textOffset;
    NumberFormat numberFormat;
    public TemperatureView(Context context) {
        super(context);
    }

    public TemperatureView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        numberFormat= ResolveUtil.getNumberFormat(1);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.temperature_bg);
        bitmapTempC = BitmapFactory.decodeResource(context.getResources(), R.drawable.temp_c);
        bitmapTempF = BitmapFactory.decodeResource(context.getResources(), R.drawable.temp_f);
        paintBg = new Paint();
        paintBg.setAntiAlias(true);
        paintProgress = new Paint();
        paintProgress.setAntiAlias(true);
        paintProgress.setColor(Color.parseColor("#1B91DF"));

        paintProgress.setStyle(Paint.Style.FILL);

        paintText = new Paint();
        paintText.setAntiAlias(true);
        paintText.setColor(Color.parseColor("#999285"));
        paintText.setTextSize(ScreenUtils.dip2px(context,15));
        paintText.setStyle(Paint.Style.FILL);


        bitmapWidth = bitmap.getWidth();
        bitmapHeight = bitmap.getHeight();
        rectFWidth = ScreenUtils.dip2px(context, 12);
        paddingBottom = ScreenUtils.dip2px(context, 54);
        paddingTop = ScreenUtils.dip2px(context, 30);
        offset = ScreenUtils.dip2px(context, 8);
        textOffset = ScreenUtils.dip2px(context, 6);

    }

    private float tempCValue=0f;
    private float tempFValue=32f;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;
        RectF rectF = new RectF();

        rectF.right = centerX + rectFWidth / 2;
        rectF.bottom = bitmapHeight * 0.79f;
        float totalHeight=bitmapHeight * 0.78f-bitmapHeight * 0.16f;

        int bitmapLeft = (getWidth() - bitmapWidth) / 2;
        int bitmapRight = (getWidth() + bitmapWidth) / 2;
        int bitmapTop = (getHeight() - bitmapHeight) / 2;
        rectF.left = centerX - rectFWidth *1.2f/ 2;


        float tempCLeft=bitmapLeft - offset - bitmapTempC.getWidth();
        float tempFLeft=bitmapRight + offset;
        float tempCTop=paddingTop;

        canvas.drawBitmap(bitmapTempC, tempCLeft, tempCTop, paintBg);//摄氏值背景
        canvas.drawBitmap(bitmapTempF, tempFLeft, tempCTop, paintBg);//华氏值背景
        String tempCString=tempCValue + "℃";
        String tempFString=numberFormat.format(tempFValue) + "℉";
        Rect rectText=new Rect();
        paintText.getTextBounds(tempCString, 0, tempCString.length(), rectText);
        canvas.drawText(tempCString,tempCLeft+bitmapTempC.getWidth()/2-rectText.width()/2-textOffset,tempCTop+bitmapTempC.getHeight()/2+rectText.height()/2, paintText);
        paintText.getTextBounds(tempFString, 0,tempFString.length(), rectText);
        canvas.drawText(tempFString,tempFLeft+bitmapTempC.getWidth()/2-rectText.width()/2+textOffset/2,tempCTop+bitmapTempC.getHeight()/2+rectText.height()/2,paintText);
        if(tempCValue>42)tempCValue=42;
        if(tempCValue<35)tempCValue=35;
        rectF.top = bitmapHeight * 0.16f+totalHeight/7f*(42-tempCValue);
        canvas.drawRect(rectF, paintProgress);//同样的颜色遮挡实际温度条的不均匀宽度
        canvas.drawBitmap(bitmap, bitmapLeft, bitmapTop, paintBg);//背景温度图
        rectF.left = centerX - rectFWidth / 2;
        canvas.drawRect(rectF, paintProgress);//温度进度条
    }

    private float getTempFValue(float tempC) {
        return 1.8f * tempC + 32;
    }

    public void setTempCValue(float tempC) {
        this.tempCValue = tempC;

        tempFValue = getTempFValue(tempCValue);
        postInvalidate();
    }

    private static final String TAG = "TemperatureView";

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(bitmapWidth + offset * 2 + bitmapTempC.getWidth() * 2, bitmapHeight);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}
